﻿using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Set.API.Application.Commands.WorkCenters
{
    /// <summary>
    /// 工作中心删除操作
    /// </summary>
    public class WorkCenterDeleteHandler : IRequestHandler<WorkCenterDeleteCommand, Result>
    {
        private readonly IBaseRepository<WorkCenter> WorkCenterRepository;
        private readonly ILogger<WorkCenterDeleteHandler> logger;

        public WorkCenterDeleteHandler(IBaseRepository<WorkCenter> workCenterRepository, ILogger<WorkCenterDeleteHandler> logger)
        {
            WorkCenterRepository = workCenterRepository;
            this.logger = logger;
        }

        public async Task<Result> Handle(WorkCenterDeleteCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Added };
            try
            {
                var sql = $"UPDATE workCenters SET IsDelWorkCenter = true WHERE WorkCenterId = {request.workCenterId};";
                var WorkCenterRes = WorkCenterRepository.Update(sql, null);
                if (WorkCenterRes > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = null;
                    result.Msg = "删除成功，操作人：" + request.name;
                    logger.LogInformation("删除成功，操作人：" + request.name);
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Delete;
                    result.Data = null;
                    result.Msg = "删除失败";
                    logger.LogError("删除成功");
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"删除操作失败,错误信息为：" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
